package com.sfilyh.util;

import java.util.ArrayList;
import java.util.List;

public class PageBean<T> {

	private int currPage;			// 当前页
	private int totalRecord;		// 总记录数 
	private int totalPage;			// 总页数
	private int pageSize;			// 页的大小
	private int prePage;			// 上一页
	private int nextPage;			// 下一页
	private boolean hasNextPage;	// 是否有下一页	
	private boolean hasPrePage;		// 是否有上一页
	private List<T> pageData;		// 当前页的数据
	
	private String preUrl;			// 上一页URL	
	private String nextUrl;			// 下一页URL
	private String homeUrl;			// 首页URL
	private String endUrl;			// 未页RUL
		
	public PageBean(){}

	public PageBean (int currPage, int totalRecord, int pageSize,
			List<T> pageData) {
		this.currPage = currPage;
		this.totalRecord = totalRecord;
		this.pageSize = pageSize;
		this.pageData = pageData;
						
		this.totalPage = this.totalRecord % this.pageSize == 0 ? this.totalRecord / this.pageSize : this.totalRecord / this.pageSize + 1 ;
		this.hasNextPage = this.currPage < this.totalPage;
		this.hasPrePage = this.currPage > 1;
		this.prePage = this.hasPrePage ? this.currPage - 1 : 1;
		this.nextPage = this.hasNextPage ? this.currPage + 1 : this.totalPage;
		if(this.currPage < 1) this.currPage = 1;
		if(this.totalPage < 1) this.totalPage =1;
	}
	
	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public List<T> getPageData() {
		return pageData;
	}

	public void setPageData(List<T> pageData) {
		this.pageData = pageData;
	}
	
	public String getPreUrl() {
		return preUrl;
	}

	public void setPreUrl(String preUrl) {
		this.preUrl = preUrl;
	}

	public String getNextUrl() {
		return nextUrl;
	}

	public void setNextUrl(String nextUrl) {
		this.nextUrl = nextUrl;
	}

	public String getHomeUrl() {
		return homeUrl;
	}

	public void setHomeUrl(String homeUrl) {
		this.homeUrl = homeUrl;
	}

	public String getEndUrl() {
		return endUrl;
	}

	public void setEndUrl(String endUrl) {
		this.endUrl = endUrl;
	}	
	
	/**
	 * 数字分布
	 * @param num  分页数据数（是基数且大于3）
	 * @return
	 */
	public List<Integer> numPage(int num){
		if(num < 3 || num % 2 == 0){
			return null;
		}
		int t = num / 2 ,
			begin = 2,
			end = this.totalPage -1,
			curr = this.getCurrPage();
		if(curr - t >= begin){
			begin = curr -t;
		}
		
		if((begin + num - 1) <= end){
			end = begin + num - 1;
		}else {
			while(begin > 2 && end - begin < 4){
				begin --;
			}
		}
		
		List<Integer> rList = new ArrayList<Integer>();
		for(int i = begin ; i <= end ; i++){
			rList.add(i);
		}
		return rList;
	}	
}
